#### My (demuredemeanor) sub source ssh-agent script
# Uses tabstop=4; shiftwidth=4 tabs; foldmarker={{{,}}};
# https://notabug.org/demure/dotfiles/
# legacy repo http://github.com/demure/dotfiles
# vim:set syntax=sh:

# Add check for key exists first
# Add check -> Y/N for ssh_env existing? might not need, if using __ssh_agent right

## SSH Agent
function __ssh_agent() {
    local SSH_ENV="${HOME}/.ssh/environment"
    local U_CHECK="$(logname)"
    logname &>/dev/null
    local F_CHECK="$?"

    function __start_agent {
        echo "Initialising new SSH agent..."
        /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
        echo succeeded
        chmod 600 "${SSH_ENV}"
        . "${SSH_ENV}" > /dev/null
        /usr/bin/ssh-add;
    }

    ## Source SSH settings, if applicable
    if [ ${F_CHECK} == 0 ] && { [ "${U_CHECK}" == "${USER}" ] || [ "${U_CHECK}" == "logname: no login name" ] ;}; then
        if [ -f "${SSH_ENV}" ]; then
            . "${SSH_ENV}" > /dev/null
            ps -ef 2>/dev/null | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
                pstree -up ${USER} 2>/dev/null | grep ${SSH_AGENT_PID} | grep ^ssh-agent > /dev/null || {
                    __start_agent;
                }
            }
          else
            __start_agent;
        fi
    fi
}
